package com.oschina.wed.service.impl;

import com.oschina.wed.common.utils.CommonUtils;
import com.oschina.wed.dal.entity.AccountVoucherFlow;
import com.oschina.wed.service.AccountVoucherService;
import com.oschina.wed.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Random;

@Component("accountVoucherService")
public class AccountVoucherServiceImpl extends BaseService implements AccountVoucherService{
    private final Logger logger = LoggerFactory.getLogger(AccountVoucherServiceImpl.class);

    @Override
    public List<AccountVoucherFlow> selectForStatics(Hashtable<String, Object> query) {
        return this.getSuperDAO().getList("AccountVoucherMapper.selectIfNotStatice", query);
    }

    @Override
    public Integer updateAccountVoucherSum(Hashtable<String, Object> data) {
        return this.getSuperDAO().update("AccountVoucherMapper.updateAccountSum", data);
    }

    @Override
    public Integer update2Staticed(String voucherNos) {
        return this.getSuperDAO().update("AccountVoucherMapper.update2staticed", voucherNos);
    }

    @Override
    public Integer insertAccountVoucherSum(Hashtable<String, Object> data) {
        return this.getSuperDAO().insert("AccountVoucherMapper.insertAccountSum", data);
    }

    @Override
    public Integer insertAccountVoucherFlow() {
        AccountVoucherFlow tmp;
        Long[] accounts = new Long[3];
        accounts[0] = 100060010001L;
        accounts[1] = 100070010002L;
        accounts[2] = 100080010003L;
        Long amount = 5000000000L;
        Long beforeAmount, payAmount, afterAmount;
        Integer successCount = 0;
        for (int i = 0; i < 500 * 1000; i++) {
            payAmount = Long.valueOf(new Random().nextInt(10000));
            beforeAmount = amount;
            afterAmount = amount -= payAmount;
            tmp = new AccountVoucherFlow();
            tmp.setAccountNo(accounts[new Random().nextInt(3)]);
            tmp.setAccountDirection(new Random().nextInt(2));
            tmp.setAmount(payAmount);
            tmp.setBeforeAmount(beforeAmount);
            tmp.setAfterAmount(afterAmount);
            tmp.setPayDate(new Date());
            tmp.setIsStatics(0);
            if (this.getSuperDAO().insert("AccountVoucherMapper.insertAccountVoucher", CommonUtils.convert2HashTable(tmp)) > 0) {
                successCount += 1;
            }
        }
        return successCount;
    }
}
